﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Web;

namespace Wechat.Util.StringUtil
{
    public class NotANumber : Exception
    {
        public NotANumber(string message)
            : base(message) { }
    }
    public class DateFormatError : Exception
    {
        public DateFormatError(string message)
            : base(message) { }
    }
    public class BeanUtil
    {
        public static T FillBean<T>(HttpRequestBase request)
        {
            PropertyInfo[] Properties = typeof(T).GetProperties();
            T bean = Activator.CreateInstance<T>();
            NameValueCollection nameVals = request.Form;
            for (int i = 0; i < nameVals.Count; i++)
            {
                string keyStr = nameVals.GetKey(i);
                StringBuilder valStr = new StringBuilder();
                string[] vals = nameVals.GetValues(i);
                if (nameVals.GetValues(i).Count() > 1)
                {
                    for (int j = 0; j < vals.Length; j++)
                    {
                        if (j < vals.Length - 1)
                        {
                            valStr.Append(vals[j] + ",");
                        }
                        else
                        {
                            valStr.Append(vals[j]);
                        }
                    }
                }
                else
                {
                    valStr.Append(vals[0]);
                }
                foreach (PropertyInfo pro in Properties)//遍历该类所有属性
                {
                    if (pro.Name.ToLower().Equals(keyStr.ToLower()))
                    {
                        pro.SetValue(bean, valStr.ToString());
                    }
                }
            }
            return bean;
        }

        public static void CopyProperties<T, W>(T source, W target)
        {
            PropertyInfo[] Properties = typeof(T).GetProperties();
            PropertyInfo[] wProperties = typeof(W).GetProperties();
            foreach (PropertyInfo wPro in wProperties)//遍历该类所有属性
            {
                foreach (PropertyInfo pro in Properties)//遍历该类所有属性
                {
                    if (wPro.Name.ToLower().Equals(pro.Name.ToLower()) && wPro.PropertyType.Equals(pro.PropertyType))
                    {
                        wPro.SetValue(target, pro.GetValue(source));
                    }
                    else if (wPro.Name.ToLower().Equals(pro.Name.ToLower()) && !wPro.PropertyType.Equals(pro.PropertyType))
                    {
                        if (wPro.PropertyType.Equals(typeof(DateTime)))
                        {
                            try
                            {
                                wPro.SetValue(target, Convert.ToDateTime(pro.GetValue(source)));
                            }
                            catch (Exception)
                            {
                                throw new DateFormatError("日期格式不正确");
                            }

                            break;
                        }
                        if (wPro.PropertyType.Equals(typeof(int)))
                        {
                            try
                            {
                                wPro.SetValue(target, Convert.ToInt32(pro.GetValue(source)));
                            }
                            catch (Exception)
                            {
                                throw new NotANumber("输入的不是一个数字");
                            }
                            break;
                        }
                        if (wPro.PropertyType.Equals(typeof(double)) || wPro.PropertyType.Equals(typeof(float)))
                        {
                            try
                            {
                                wPro.SetValue(target, Convert.ToDouble(pro.GetValue(source)));
                            }
                            catch (Exception)
                            {
                                throw new NotANumber("输入的不是一个数字");
                            }
                            break;
                        }
                    }
                }
            }
        }
    }

}
